using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Agriculture.Api.Read.Application.Command;
using Agriculture.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Agriculture.Api.Read.Controllers
{
    /// <summary>
    /// 药品订单管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class MedicineOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public MedicineOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取药品订单列表
        /// </summary>
        /// <param name="orderSearch">订单号或收货人</param>
        /// <param name="startTime">下单开始时间</param>
        /// <param name="endTime">下单结束时间</param>
        /// <param name="status">状态（0：全部，1：待发货，2：待收货，3：已收货）</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>药品订单列表</returns>
        [HttpGet]
        public async Task<ApiPageing<MedicineOrderDto>> GetMedicineOrders(
            [FromQuery] string orderSearch = null,
            [FromQuery] DateTime? startTime = null,
            [FromQuery] DateTime? endTime = null,
            [FromQuery] int? status = null,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var query = new MedicineOrderQuery
            {
                OrderSearch = orderSearch,
                StartTime = startTime,
                EndTime = endTime,
                Status = status,
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            var result = await _mediator.Send(query);
            
            return new ApiPageing<MedicineOrderDto>
            {
                Code = ApiEnum.Success,
                Msg = "查询成功",
                TotaCount = result.TotalCount,
                TotaPage = result.TotalPages,
                Data = null,
                PageData = result.MedicineOrders
            };
        }

        /// <summary>
        /// 获取药品订单详情
        /// </summary>
        /// <param name="orderNo">订单编号</param>
        /// <returns>订单详情</returns>
        [HttpGet("{orderNo}")]
        public async Task<ApiResult<MedicineOrderDetailResult>> GetMedicineOrderDetail(string orderNo)
        {
            var query = new MedicineOrderDetailQuery
            {
                OrderNo = orderNo
            };

            var result = await _mediator.Send(query);
            
            return new ApiResult<MedicineOrderDetailResult>
            {
                Code = result == null ? ApiEnum.Fail : ApiEnum.Success,
                Msg = result == null ? "未找到该订单记录" : "查询成功",
                Data = result
            };
        }

        /// <summary>
        /// 获取物流信息
        /// </summary>
        /// <param name="orderNo">订单编号</param>
        /// <returns>物流信息</returns>
        [HttpGet("logistics/{orderNo}")]
        public async Task<ApiResult<LogisticsTrackingResult>> GetLogisticsTracking(string orderNo)
        {
            var query = new LogisticsTrackingQuery
            {
                OrderNo = orderNo
            };

            var result = await _mediator.Send(query);
            
            return new ApiResult<LogisticsTrackingResult>
            {
                Code = result == null ? ApiEnum.Fail : ApiEnum.Success,
                Msg = result == null ? "未找到该订单的物流信息" : "查询成功",
                Data = result
            };
        }
    }
} 